package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseConstant;

/**
 * description: 修改数据, 上下2个, 构成一个区间值
 * created by kalu on 2018/4/16 13:15
 */
public class InputThreeDialog extends BaseDialog implements View.OnClickListener {

    private String in, out, night;
    /************************************************/

    private OnDialogChangeListener listener;

    public InputThreeDialog(Activity activity) {
        super(activity);
    }

    @Override
    public void show() {
        super.show();
//        setCanceledOnTouchOutside(false);
//        setCancelable(false);
    }

    @Override
    public void dismiss() {

        if (null != listener && (!TextUtils.isEmpty(in) || !TextUtils.isEmpty(out) || !TextUtils.isEmpty(night))) {
            listener.onDialogChange(in, out, night);
        }

        super.dismiss();
    }

    @Override
    public void cancel() {

        if (null != listener && (!TextUtils.isEmpty(in) || !TextUtils.isEmpty(out) || !TextUtils.isEmpty(night))) {
            listener.onDialogChange(in, out, night);
        }

        super.cancel();
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_input_three;
    }

    @Override
    protected void initData() {

        findViewById(R.id.dialog_assay_reference_back).setOnClickListener(this);
        findViewById(R.id.dialog_assay_reference_ok).setOnClickListener(this);

        final EditText edit1 = findViewById(R.id.dialog_input_three_edit1);
        if (null != edit1) {
            in = edit1.getText().toString().trim();
            edit1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    in = editable.toString();
                }
            });
        }

        final EditText edit2 = findViewById(R.id.dialog_input_three_edit2);
        if (null != edit2) {
            out = edit2.getText().toString().trim();
            edit2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    out = editable.toString();
                }
            });
        }

        final EditText edit3 = findViewById(R.id.dialog_input_three_edit3);
        if (null != edit3) {
            night = edit1.getText().toString().trim();
            edit3.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    night = editable.toString();
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_assay_reference_back:
                in = BaseConstant.NULL_STR;
                out = BaseConstant.NULL_STR;
                night = BaseConstant.NULL_STR;
                dismiss();
                break;
            case R.id.dialog_assay_reference_ok:

                dismiss();
                break;
        }
    }

    public void setInputText(String in, String out, String night) {

        final EditText text1 = findViewById(R.id.dialog_input_three_edit1);
        text1.setText(in);
        text1.setSelection(in.length());

        final EditText text2 = findViewById(R.id.dialog_input_three_edit2);
        text2.setText(out);
        text2.setSelection(out.length());

        final EditText text3 = findViewById(R.id.dialog_input_three_edit3);
        text3.setText(night);
        text3.setSelection(night.length());
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {

        void onDialogChange(String in, String out, String night);
    }
}
